



//
//  NFillDataViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/14.
//  Copyright © 2018 macfai. All rights reserved.
#import "NFillDataViewController.h"
#import "NDataCell.h"
#import "NMineViewController.h"
//#import "UIControl+recurClick.h"
#import "UIButton+touch.h"
@interface NFillDataViewController ()<UITableViewDelegate,UITableViewDataSource,SHAddImgViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSArray *titles;

//营业执照存放数组
@property (nonatomic,strong)NSArray *photos;

//营业执照附件存放数组
@property (nonatomic,strong)NSArray *photosCopy;

///企业名称
@property (nonatomic,copy)NSString *enterPriseName;

@property (nonatomic,strong)NSMutableArray *zhizhaoArray;

@property (nonatomic,copy)NSString *enterPriseCode;
@property (nonatomic,copy)NSString *contactName;
@property (nonatomic,copy)NSString *tel;

@property (nonatomic,assign)NSInteger cellIndex;


@property (nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation NFillDataViewController
#define cellIdentifier @"datacell"
-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NDataCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [NGlobalModel shareInstance].isAuth = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     [NGlobalModel shareInstance].isAuth = NO;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"资料填写" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.zhizhaoArray = [NSMutableArray array];
    self.cellIndex = 0;
    self.dataArray = [NSMutableArray array];
    self.titles = @[@"企业名称",@"统一社会信用代码",@"联系人姓名",@"联系人电话",@"",@""];
    [self.view addSubview:self.tableView];
    [self addFooter];
//    [self loadData];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)addFooter {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100.f)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 30, ScreenWidth-60, 50);
    btn.centerX = footer.centerX;
    btn.uxy_acceptEventInterval = 3.f;
    btn.backgroundColor = ThemeBlue;
    btn.layer.cornerRadius = 5.f;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    [btn addTarget: self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.uxy_acceptEventInterval = 3.f;
    [footer addSubview:btn];
    self.tableView.tableFooterView = footer;
}

#pragma mark - 认证提交
- (void)tapAction:(UIButton *)btn
{
    //认证状态  登录后返回字段  userInfoCertificationStatus  0审核中  1已认证  2未通过   3未认证
    //identType  认证类型  0个人  1企业
    if (self.enterPriseName.length==0||self.tel.length==0) {
        [AFToast showText:@"请填写认证资料"];
        return;
    }
    
    
    NSDictionary *dic =  @{@"identId":[NUserModel shareInstance].userInfoId?:@"1",@"identType":[NUserModel shareInstance].userInfoType,@"identEnterpriseName":self.enterPriseName,@"identEnterpriseCode":self.enterPriseCode,@"identContact":self.contactName,@"identTelephone":self.tel};
    
    //图片资源不提交也可以成功
    [NNetworkClient uploadImage:self.photos url:kAccoutAuthUrl parameters:dic success:^(id responseObject) {
        
        YYLog(@"%@",responseObject);
        WeakStament(ws);
        if ([responseObject[@"code"] integerValue]==0) {
            [AFToast showText:@"认证提交成功,请等待审核结果"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                //跳回到指定页面
//                NSArray * ctrlArray = self.navigationController.viewControllers;
//                [ws.navigationController popToViewController:ctrlArray[1] animated:YES];
                [ws.navigationController popToRootViewControllerAnimated:YES];
                //下面的写法直接崩溃
//                NMineViewController *mine = [NMineViewController new];
//                [ws.navigationController popToViewController:mine animated:YES];
            });
        }
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        
    } delegater:nil];

}


#pragma mark - delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row>3) {
        return 130.f;
    }else{
        return 60.f;
    }
    return 0;
}


#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NDataCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.titleLabl.text = self.titles[indexPath.row];
    
//    cell.dataTF.delegate = self;
   
    if (indexPath.row>3) {
        cell.dataTF.hidden = YES;
    }
    
    if (indexPath.row==4||indexPath.row==5) {
        [self setUIWithCell:cell withIndexPath:indexPath];
    }
    
    ///通过tag值来标记一个table中不同的行的textfield
    
    cell.dataTF.tag = indexPath.row+200;
    //记住设置代理
    cell.dataTF.delegate = self;
    
    //字数实时监听
    [cell.dataTF addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
    
    //底部线条
//    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(5,indexPath.row>3?129:59, ScreenWidth-10, 0.5)];
//    line.backgroundColor = kGrayColor;
//    [cell addSubview:line];
    return cell;
}
///获取cell的textField输入的内容
- (void)textFieldWithText:(UITextField *)tf{
    
    switch (tf.tag) {
        case 200:
        {
            self.enterPriseName = tf.text;
            [NGlobalModel shareInstance].qiyeName = self.enterPriseName;
            [self restrictWordNumber:tf];
            YYLog(@"%@",tf.text);
        }
            break;
        case 201:
        {
            self.enterPriseCode = tf.text;
           
            [NGlobalModel shareInstance].uniformCode = self.enterPriseCode;
             [self restrictWordNumber:tf];
            YYLog(@"%@",tf.text);
        }
            break;
        case 202:
        {
            self.contactName = tf.text;
           
            [NGlobalModel shareInstance].contact = self.contactName;
             [self restrictWordNumber:tf];
            YYLog(@"%@",tf.text);
        }
            break;
        case 203:
        {
            self.tel = tf.text;
           
            [NGlobalModel shareInstance].tel = self.tel;
             [self restrictWordNumber:tf];
            YYLog(@"%@",tf.text);
        }
            break;
        default:
            break;
    }
}
///设置cell 内容
- (void)setUIWithCell:(NDataCell *)cell withIndexPath:(NSIndexPath *)indexPath{
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(23,15,200,18)];
    if (indexPath.row==4) {
        label.text =@"营业执照原件";
        self.cellIndex = indexPath.row;
    }else{
         label.text =@"营业执照复印件(加盖公章)";
        self.cellIndex = indexPath.row;
    }
    label.textColor = kDarkGrayColor;
    label.font = kFont15;
    [cell.contentView addSubview:label];
    [self addPhotoAreaOnCell:cell];
    
}

///添加照片选择区域
- (void)addPhotoAreaOnCell:(NDataCell *)cell{
    WeakStament(ws);
    if (self.cellIndex==4) {
        SHAddImgView *addImgView = [[SHAddImgView alloc] initWithMaxColumn:4];
        addImgView.frame = CGRectMake(22, 33, ScreenWidth, 95);
        addImgView.configModel = [SHConfigModel creatNormalConfig];
        addImgView.backgroundColor = kWhiteColor;
        addImgView.delegate = self;
        addImgView.ImgBlock = ^(NSArray *photos) {
            YYLog(@"%@",photos);
            ws.photos = photos;
            [ws.zhizhaoArray addObject:[photos lastObject]];
        };
        //    _addImgView = addImgView;
        
        [cell.contentView addSubview:addImgView];
        
        
        
    }else{
        SHAddImgView *addImgView = [[SHAddImgView alloc] initWithMaxColumn:4];
        addImgView.frame = CGRectMake(22, 33, ScreenWidth, 95);
        addImgView.configModel = [SHConfigModel creatNormalConfig];
        addImgView.backgroundColor = kWhiteColor;
        addImgView.delegate = self;
        addImgView.ImgBlock = ^(NSArray *photos) {
            YYLog(@"%@",photos);
            self.photos = photos;
             [ws.zhizhaoArray addObject:[photos lastObject]];
        };
        //    _addImgView = addImgView;
        [cell.contentView addSubview:addImgView];
    }
   
}

///
- (void)restrictWordNumber:(UITextField *)textField{
    
    if (textField.tag==200) {
        
        if (textField.text.length > 50) {
            UITextRange *markedRange = [textField markedTextRange];
            if (markedRange) {
                return;
            }
            //Emoji占2个字符，如果是超出了半个Emoji，用15位置来截取会出现Emoji截为2半
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:50];
            textField.text = [textField.text substringToIndex:range.location];
        }
    }else if (textField.tag==201){
        
        if (textField.text.length > 18) {
            UITextRange *markedRange = [textField markedTextRange];
            if (markedRange) {
                return;
            }
            NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:18];
            textField.text = [textField.text substringToIndex:range.location];
        }
    }else if (textField.tag==202){
        
        if (textField.text.length > 10) {
            UITextRange *markedRange = [textField markedTextRange];
            if (markedRange) {
                return;
            }
            NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:10];
            textField.text = [textField.text substringToIndex:range.location];
        }
        
    }else if(textField.tag==203){
        //进行手机号合法性判断
//        if (![ToolKit validateMobile:textField.text]) {
//            [AFToast showText:@"请输入正常的电话号码"];
//            textField.text = @"";
//            return;
//        }
//        textField.keyboardType = UIKeyboardTypePhonePad;
        
    }
    
}

///进行手机号的校验
-(void)textFieldDidEndEditing:(UITextField *)textField{
 
    if (textField.tag==203) {
        if (![ToolKit validateMobile:textField.text]) {
            [AFToast showText:@"请输入正确的电话号码"];
            textField.text = @"";
            return;
        }

    }
}

@end
